package com.example.treasure_back.controller;

import com.example.treasure_back.entity.MsgEntity;
import com.example.treasure_back.model.UserClockModel;
import com.example.treasure_back.service.UserClockService;
import com.example.treasure_back.shiroEntity.auth.AuthUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/treasure/userClock")
public class UserClockController {
    @Autowired
    private UserClockService userClockService;

    @RequestMapping("/getTotalDaysByEmail")
    public MsgEntity getTotalDaysByEmail(@AuthUser String email) {
        UserClockModel userClock = userClockService.getUserClock(email);
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        // 今日时间和数据存的时间进行比较，不同更新打卡信息
        if (!format.format(userClock.getToday()).equals(format.format(date))) {
            userClockService.updateDate(email);
            userClock = userClockService.getUserClock(email);
        }
        userClock.setToday(null);
        return new MsgEntity("SUCCESS", "200", userClock);
    }

    @RequestMapping("/changeClock")
    public MsgEntity changeClock(@AuthUser String email){
        userClockService.changeClock(email);
        return new MsgEntity("SUCCESS","200","ok");
    }
}
